﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Sharp.DataTransferObject;
using Sharp.Domain.Model;
using Sharp.Domain.PlatformBoundedContext.MessageModule.Aggregates;
using Sharp.Infrastructure;
using Sharp.Infrastructure.Crosscutting;
using Sharp.Infrastructure.Search;

namespace Sharp.Application.Platform
{

    public partial class UserServiceImpl
    {
        /// <summary>
        /// 发送通知
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        [Logging("发送通知")]
        public Result SendNotice(NoticeDTO model)
        {
            Result res = new Result();

            var info = model.To<Notice>();
            info.PublishTime = DateTime.Now;


            _noticeRepository.Add(info);


            Context.Commit();
            return res;
        }

        /// <summary>
        /// 删除通知
        /// </summary>
        /// <param name="ids"></param>
        /// <returns></returns>
        [Logging("删除通知“{@p0}”")]
        public Result DeleteNotices(Guid[] ids)
        {
            var res = new Result();
            if (ids == null)
            {
                res.Err = "Id参数不可为空！";
                return res;
            }

            _noticeRepository.Remove(ids);
            Context.Commit();
            return res;
        }
        /// <summary>
        /// 获取通知
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public NoticeDTO GetNoticeInfo(Guid id)
        {
            return _noticeRepository.Get(id).To<NoticeDTO>();
        }

        /// <summary>
        /// 未读通知
        /// </summary>
        /// <returns></returns> 
        public Result<IList<NoticeDTO>> NoReadNotices()
        {
            var list = _noticeRepository.GetList(x => !x.IsReaded && x.ReceiverId == ContactContext.Current.UserId,
                10).To<List<NoticeDTO>>();

            return new Result<IList<NoticeDTO>>()
            {
                Data = list
            };
        }
        /// <summary>
        /// 获取通知
        /// </summary>
        /// <param name="pf"></param>
        /// <returns></returns>
        public PagedResult<NoticeDTO> GetNoticePaged(PagingFeature pf)
        {
            var paged = _noticeRepository.Pagging(pf, x => x.ReceiverId == ContactContext.Current.UserId);

            var output = paged.To<Notice, NoticeDTO>();

            return output;
        }
    }
}
